function MoveUp(oActive)
{
if (oActive != null)
{
switch (oActive.className)
{
case "section":

var oSection = oActive.parentElement.parentElement;
if (oSection.previousSibling)
{


MoveUpAdjacentXml(GetSectionNode(oActive.name));



oSection.swapNode(oSection.previousSibling);
}

break;

case "cell":
case "iframe":
MoveUpCell(oActive);

break;
}
}
else
{
alert(LOCID_FORMED_MUSTSELECT);
}
}

function MoveUpCell(oActive)
{
var oCell = GetCellNode(oActive.name);
var oRow = oCell.parentNode;
var oRows = oRow.parentNode;
var oSection = oRows.parentNode;
var layout = oSection.getAttribute("layout");
var columns = oSection.getAttribute("columns");
var sectionName = oSection.getAttribute("name");
var bRefresh = false;
if(layout == null)
{
layout = "varwidth";
columns = "11";
}

if(layout == "varwidth")
{
if (oActive.parentElement.previousSibling.rowIndex > 0)
{


if ((oActive.parentElement.cells.length == 1) || (oActive.parentElement.previousSibling.cells.length != 2))
{
MoveUpRows(oActive);

bRefresh = true;

}
else
{


var oNode = GetCellNode(oActive.name);

var oParentNode		= oNode.parentNode;
var oPrevParentNode	= oParentNode.previousSibling;
var oNextParentNode;

if (oNode.nextSibling)
{
oParentNode.insertBefore(oPrevParentNode.removeChild(oPrevParentNode.childNodes[0]), oNode.nextSibling);

oNode = oPrevParentNode.childNodes[0];

oParentNode		= oNode.parentNode;
oNextParentNode	= oParentNode.nextSibling;

oParentNode.insertBefore(oNextParentNode.removeChild(oNextParentNode.childNodes[0]), oNode);
}
else
{
oParentNode.insertBefore(oPrevParentNode.removeChild(oPrevParentNode.lastChild), null);

oNode = oPrevParentNode.childNodes[0];

oParentNode		= oNode.parentNode;
oNextParentNode	= oParentNode.nextSibling;

oParentNode.insertBefore(oNextParentNode.removeChild(oNextParentNode.lastChild.previousSibling), null);
}

DeleteEmptyRows(oRows);
bRefresh = true;
}
}
}
else
{
CreateSectionSchema(sectionName,oRows,columns);
bRefresh = MoveUpInSchema(oActive.name);
if(bRefresh)
{
RegenerateXmlFromSchema(oRows);
}

}

if(bRefresh)
{
RefreshSectionHtml(oActive.parentElement.parentElement.parentElement,oActive.name);
}
}

function MoveUpRows(oActive)
{
var oNode = GetCellNode(oActive.name);
var rowSpan = GetRowSpan(oNode);
var oRow = oNode.parentNode;
var oRows = oRow.parentNode;

var nextSibling = oRow;
for (var i = 0 ; i < rowSpan ; i++)
{
nextSibling = nextSibling.nextSibling;
}

var oInsertRow = oRow.previousSibling;
var oPrevRow = oInsertRow.previousSibling;
do
{
oRows.insertBefore(oRows.removeChild(oInsertRow), nextSibling);
nextSibling = oInsertRow;
oInsertRow = oPrevRow;
if(oPrevRow) oPrevRow = oPrevRow.previousSibling;
}
while(nextSibling.childNodes.length == 0);


}

function MoveDown(oActive)
{
if (oActive != null)
{
switch (oActive.className)
{
case "section":
var oSection = oActive.parentElement.parentElement;

if (oSection.nextSibling && oSection.nextSibling.cells[0].childNodes.length > 0)
{


MoveDownAdjacentXml(GetSectionNode(oActive.name));



oSection.swapNode(oSection.nextSibling);
}

break;

case "cell":
case "iframe":
MoveDownCell(oActive);
break;
}
}
else
{
alert(LOCID_FORMED_MUSTSELECT);
}
}

function MoveDownCell(oActive)
{
var oCell = GetCellNode(oActive.name);
var oRow = oCell.parentNode;
var oRows = oRow.parentNode;
var oSection = oRows.parentNode;
var layout = oSection.getAttribute("layout");
var columns = oSection.getAttribute("columns");
var sectionName = oSection.getAttribute("name");
var bRefresh = false;
if(layout == null)
{
layout = "varwidth";
columns = "11";
}

if(layout == "varwidth")
{
if (oActive.parentElement.nextSibling)
{


if ((oActive.parentElement.cells.length == 1) || (oActive.parentElement.nextSibling.cells.length != 2))
{

var oNextRow = oActive.parentElement.nextSibling ;
while(oNextRow && oNextRow.cells.length == 0)
{
oNextRow = oNextRow.nextSibling;
}

if(oNextRow)
{
if(typeof(oNextRow.cells[0].name) != "undefined")
{
MoveUpRows(oNextRow.cells[0]);
}
else
{
MoveUpRows(oNextRow.cells[1]);
}
bRefresh = true;
}


}
else
{


var oNode = GetCellNode(oActive.name);

var oParentNode		= oNode.parentNode;
var oNextParentNode	= oParentNode.nextSibling;
var oPrevParentNode;

if (oNode.nextSibling)
{
oParentNode.insertBefore(oNextParentNode.removeChild(oNextParentNode.childNodes[0]), oNode.nextSibling);

oNode = oNextParentNode.childNodes[0];

oParentNode		= oNode.parentNode;
oPrevParentNode	= oParentNode.previousSibling;

oParentNode.insertBefore(oPrevParentNode.removeChild(oPrevParentNode.childNodes[0]), oNode);
}
else
{
oParentNode.insertBefore(oNextParentNode.removeChild(oNextParentNode.lastChild), oNode);

oNode = oNextParentNode.childNodes[0];

oParentNode		= oNode.parentNode;
oPrevParentNode	= oParentNode.previousSibling;

oParentNode.insertBefore(oPrevParentNode.removeChild(oPrevParentNode.lastChild), null);
}

DeleteEmptyRows(oRows);
bRefresh = true;
}
}
}
else
{
CreateSectionSchema(sectionName,oRows,columns);
bRefresh = MoveDownInSchema(oActive.name);
if(bRefresh)
{
RegenerateXmlFromSchema(oRows);
}

}

if(bRefresh)
{
RefreshSectionHtml(oActive.parentElement.parentElement.parentElement,oActive.name);
}

}


function MoveLeft(oActive)
{
if (oActive != null)
{
if (oActive.parentElement.className == "ms-crm-Tab ms-crm-Tab-Selected")
{
oActive = oActive.parentElement;
}
switch (oActive.className)
{
case "ms-crm-Tab ms-crm-Tab-Selected":
if (oActive.previousSibling)
{


MoveUpAdjacentXml(GetTabNode(oActive.name));



oActive.swapNode(oActive.previousSibling);
}
break;

case "cell":
case "iframe":

MoveLeftCell(oActive);

break;
}
}
else
{
alert(LOCID_FORMED_MUSTSELECT);
}
}

function MoveLeftCell(oActive)
{
var oCell = GetCellNode(oActive.name);
var oRow = oCell.parentNode;
var oRows = oRow.parentNode;
var oSection = oRows.parentNode;
var layout = oSection.getAttribute("layout");
var columns = oSection.getAttribute("columns");
var sectionName = oSection.getAttribute("name");
var bRefresh = false;
if(layout == null)
{
layout = "varwidth";
columns = "11";
}

if(layout == "varwidth")
{


if (oActive.previousSibling && oActive.parentElement.cells.length > 1)
{


if (oActive.name != "")
{
MoveUpAdjacentXml(GetCellNode(oActive.name));
}
else
{
MoveUpAdjacentXml(GetCellNode(oActive.previousSibling.name).nextSibling);
}



oActive.swapNode(oActive.previousSibling);
}
}
else
{
CreateSectionSchema(sectionName,oRows,columns);
bRefresh = MoveLeftInSchema(oActive.name);
if(bRefresh)
{
RegenerateXmlFromSchema(oRows);
}
}

if(bRefresh)
{
RefreshSectionHtml(oActive.parentElement.parentElement.parentElement,oActive.name);
}

}
function MoveRight(oActive)
{
if (oActive != null)
{
if (oActive.parentElement.className == "ms-crm-Tab ms-crm-Tab-Selected")
{
oActive = oActive.parentElement;
}
switch (oActive.className)
{
case "ms-crm-Tab ms-crm-Tab-Selected":
if (oActive.nextSibling)
{


MoveDownAdjacentXml(GetTabNode(oActive.name));



oActive.swapNode(oActive.nextSibling);
}

break;

case "cell":
case "iframe":

MoveRightCell(oActive);
break;


}
}
else
{
alert(LOCID_FORMED_MUSTSELECT);
}
}

function MoveRightCell(oActive)
{
var oCell = GetCellNode(oActive.name);
var oRow = oCell.parentNode;
var oRows = oRow.parentNode;
var oSection = oRows.parentNode;
var layout = oSection.getAttribute("layout");
var columns = oSection.getAttribute("columns");
var sectionName = oSection.getAttribute("name");
var bRefresh = false;
if(layout == null)
{
layout = "varwidth";
columns = "11";
}

if(layout == "varwidth")
{


if (oActive.nextSibling && oActive.parentElement.cells.length > 1)
{


if (oActive.name != "")
{
MoveDownAdjacentXml(GetCellNode(oActive.name));
}
else
{
MoveDownAdjacentXml(GetCellNode(oActive.nextSibling.name).previousSibling);
}



oActive.swapNode(oActive.nextSibling);
}
}
else
{
CreateSectionSchema(sectionName,oRows,columns);
bRefresh = MoveRightInSchema(oActive.name);
if(bRefresh)
{
RegenerateXmlFromSchema(oRows);
}
}

if(bRefresh)
{
RefreshSectionHtml(oActive.parentElement.parentElement.parentElement,oActive.name);
}

}


function MoveUpAdjacentXml(oNode)
{
var oParentNode = oNode.parentNode;

oParentNode.insertBefore(oParentNode.removeChild(oNode.previousSibling), oNode.nextSibling);
}




function MoveDownAdjacentXml(oNode)
{
var oParentNode = oNode.parentNode;

oParentNode.insertBefore(oParentNode.removeChild(oNode.nextSibling), oNode);
}


function  GetRowSpan(oCellNode)
{
var iRowSpan = 1;
var rowSpan = oCellNode.getAttribute("rowspan");
if( rowSpan != null )
{
iRowSpan  = parseInt(rowSpan,10);
}

return iRowSpan;
}

function  GetColSpan(oCellNode)
{
var iRowSpan = 1;
var rowSpan = oCellNode.getAttribute("colspan");
if( rowSpan != null )
{
iRowSpan  = parseInt(rowSpan,10);
}

return iRowSpan;
}


function insertAfter(newElement,currentElement)
{
var nextElement;
if (nextElement = currentElement.nextSibling)
{
currentElement.parentNode.insertBefore(newElement, nextElement);
}
else
{
currentElement.parentNode.appendChild(newElement);
}
}


function DeleteEmptyRows(oRows)
{
var iRows = oRows.childNodes.length;
for(var i = 0 ; i < iRows ; i++)
{
var oRow = oRows.childNodes[i];
if(oRow != null && oRow.childNodes.length == 2 &&
oRow.selectSingleNode("cell/control") == null)
{
oRows.removeChild(oRow);

break;
}
}
}
